package com.project.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.project.db.SQLHelper;
import com.project.model.Jobs;

/**
 * Servlet implementation class ChooseJob
 */
@WebServlet("/ChooseJob")
public class ChooseJob extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChooseJob() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Cookie[] cookies = request.getCookies();
		String id = "";
		for (Cookie c : cookies) {
			if (c.getName().equals("userid")) {
				id = c.getValue();
			}
		}
		String jobs = request.getParameter("job");
		String job = jobs.split(" ")[0];
		String conpany = jobs.split(" ")[1];
		Jobs oJobs = new Jobs();
		oJobs.setUserid(new Integer(id));
		oJobs.setState(0);
		oJobs.setCompany(conpany);
		oJobs.setJobs(job);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		oJobs.setDate(df.format(new Date()));
		try {
			int result = SQLHelper.addJobs(oJobs);
			response.getWriter().println(result);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.getWriter().println("0");
		}

	}

}
